package bishi.youkushengshi;

import java.util.*;

public class demo2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int nums = in.nextInt();
        for (int i = 0; i < nums; i++) {
            int n = in.nextInt();
            int[][] total = new int[n][2];
            for (int j = 0; j < n; j++) {
                total[j][0] = in.nextInt();
                total[j][1] = in.nextInt();
            }
            int[] dp = new int[n];
            int ret = 0;

            dp[0] = 1;
            for (int k = 1; k < n; k++) {
                dp[k] = 1;
                for (int m = 0; m < k; m++) {
                    if (total[k][0] != total[m][0] && total[k][1] != total[m][1]) {
                        dp[k] = Math.max(dp[k], dp[m] + 1);
                    }

                }
                ret = Math.max(ret,dp[k]);
            }

            System.out.println(ret);
        }

    }
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        int nums = in.nextInt();
        while(nums > 0) {
            int total = in.nextInt();
            int c = in.nextInt();
            int a = in.nextInt();
            int ret = 1;

            for(int i = 1;i < total;i++) {
                int curC = in.nextInt();
                int curA = in.nextInt();
                if(curC != c && curA != a) {
                    ret++;
                    curC = c;
                    curA = a;
                }
            }
            System.out.println(ret);
            nums--;
        }
    }
}
